package com.pf.java.File;

import java.io.*;
import java.util.Scanner;

/**
 * 重定向
 *
 * ClassName:IO8
 * Package:com.pf.java.File
 *
 * @author: pf
 * @date: 2021/11/7-20:45
 */
public class IO8 {

    public static void main(String[] args) {
//        testRedirectOutPut();

        testRedirectInput();
    }

    public static void testRedirectOutPut() {
        try (
                PrintStream ps = new PrintStream(
                        new FileOutputStream("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/out.txt"));
                ){
            System.setOut(ps);
            System.out.println("龙宝sb");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void testRedirectInput() {
        try (
                FileInputStream fis = new FileInputStream("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/out.txt");
                ){
                System.setIn(fis);
                Scanner scanner = new Scanner(System.in);
                while (scanner.hasNext()) {
                    System.out.println(scanner.next());
                }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
